function doAjaxByPostForJsonResult(ajaxURL, formData, successFunction) {
	var requestData = formData.serialize();
	//ajax communication : ajaxURL
	$.ajax({type:"POST",url:ajaxURL,data:requestData,success:successFunction});
	return true;
}

function JSONtoString(object) {
	var results = [];
	for (var property in object) {
		var value = object[property];
		if (value) 
			results.push(property.toString() + ': ' + value);
	}
	return '{' + results.join(', ') + '}';
}

function sampleAjaxSuccessFunction(obj) {
	alert(obj.result);
}

function categoryListUpFunction(obj) {
	var tableContents = [];
	var selectedCate = [];
	var categoryListTemplate = "<input type=\"checkbox\" groupName= \"category\" name=\"@DATA1\" id=\"@DATA2\" cateSeq=\"@DATA3\" @DATA4 onclick=\"checkboxSelected();\" /><label for=\"@DATA5\">@DATA6</label><br/>";
	tableContents.push("<img src=\"img/title-category.png\" alt=\"카테고리\" style=\"margin-bottom: 10px;display:block;\" />");
	tableContents.push("<input groupName= \"category\" id=\"category-all\" name=\"category-all\" cateSeq=\"0\" type=\"checkbox\" onclick=\"checkboxSelectAll();\" /><label for=\"category-all\">전체보기</label><br/>");
	jQuery.each(obj.data, function(i,n) {
		var	eachCategory = categoryListTemplate;
		var   categoryCSSNm = "category-" + n['cateSeq'];
		eachCategory = eachCategory.replace("@DATA1",categoryCSSNm);
		eachCategory = eachCategory.replace("@DATA2",categoryCSSNm);
		eachCategory = eachCategory.replace("@DATA3",n['cateSeq']);
		if(n['selectedYn'] == '1') {
			eachCategory = eachCategory.replace("@DATA4","checked");
			selectedCate.push(n['cateSeq']);
		} else {
			eachCategory = eachCategory.replace("@DATA4","");
		}
		eachCategory = eachCategory.replace("@DATA5",categoryCSSNm);
		eachCategory = eachCategory.replace("@DATA6",n['cateNm']);
		tableContents.push(eachCategory);
	});
	$('input[id=selectedCate]').val(selectedCate.join(','));
	$('div[id=calendar-category]').html(tableContents.join(''));
	doAjaxByPostForJsonResult("loadSchedule.ajax", $("#userAndCalInfo"), showScheduleSampleFunction);
}

function checkboxSelected() {
	var selectedCate = [];
	$('input[groupName=category]').each(function() {
		if($(this).attr('checked') == "checked") {
			selectedCate.push($(this).attr('cateSeq'));
		}
	});
	$('input[id=selectedCate]').val(selectedCate.join(','));
	loadScheduleByCateClick();
}

function checkboxSelectAll() {
	var selectedCate = [];
	if($('input[cateSeq=0]').attr('checked') == undefined) {
		$('input[groupName=category]').each(function() {
			$(this).removeAttr("checked");
		});
	} else {
		$('input[groupName=category]').each(function() {
			$(this).attr('checked',"checked");
		});
	}
	checkboxSelected();
}

function loginInfoSetupFunction(obj) {
	jQuery.each(obj.data, function(i,n) {
		$('input[id=userSeq]').val(n['userSeq']);
		$('input[id=userType]').val(n['userType']);
	});
	doAjaxByPostForJsonResult("loadCategory.ajax", $("#userAndCalInfo"), categoryListUpFunction);
}

function showScheduleSampleFunction(obj) {
	$('#calendar').fullCalendar( 'removeEvents' );
	$('#calendar').fullCalendar( 'addEventSource', obj.data );
	$("input[id=calendarScheduleSeq]").val('');
}

function loadSchedule(frdt, todt) {
	//ssamba75 : 이벤트로딩 
	//fromto셋팅
	$("input[id=scheduleFrDt]").val(frdt);
	$("input[id=scheduleToDt]").val(todt);
	//카테고리셋팅 
	var selectedCate = [];
	$('input[groupName=category]').each(function() {
		if($(this).attr('checked') == "checked") {
			selectedCate.push($(this).attr('cateSeq'));
		}
	});			
	$('input[id=selectedCate]').val(selectedCate.join(','));
	//로딩 
	doAjaxByPostForJsonResult("loadSchedule.ajax", $("#userAndCalInfo"), showScheduleSampleFunction);
}

function loadScheduleByCateClick() {
	//ssamba75 : 이벤트로딩 
	//카테고리셋팅 
	var selectedCate = [];
	$('input[groupName=category]').each(function() {
		if($(this).attr('checked') == "checked") {
			selectedCate.push($(this).attr('cateSeq'));
		}
	});			
	$('input[id=selectedCate]').val(selectedCate.join(','));
	//로딩 
	doAjaxByPostForJsonResult("loadSchedule.ajax", $("#userAndCalInfo"), showScheduleSampleFunction);
}

function makeSchedule() {
	if($('input[id=userSeq]').val() != '0' && $('input[id=userSeq]').val() != undefined && $('input[id=userSeq]').val() != '') {
		$('input[id=newUserSeq]').val($('input[id=userSeq]').val());
		if($('input[id=newTitle]').val() == '' || $('input[id=newTitle]').val() == ' ') {
			alert("타이틀을 입력하십시오");
			$('input[id=newTitle]').focus();
			return false;
		}
		if($('input[id=newScheduleFrDt]').val() == '' || $('input[id=newScheduleFrDt]').val() == ' ') {
			alert("시작일자를 입력하십시오");
			$('input[id=newScheduleFrDt]').focus();
			return false;
		}
		if($('input[id=newScheduleToDt]').val() == '' || $('input[id=newScheduleToDt]').val() == ' ') {
			alert("종료일자를 입력하십시오");
			$('input[id=newScheduleToDt]').focus();
			return false;
		}
		if($('input[id=newScheduleLinkUrl]').val() == '' || $('input[id=newScheduleLinkUrl]').val() == ' ') {
			alert("URL을 입력하십시오");
			$('input[id=newScheduleLinkUrl]').focus();
			return false;
		}
		doAjaxByPostForJsonResult("makeSchedule.ajax", $("#makeScheduleForm"), afterMakeScheduleFunction);
		$('select[id=newCategory]').attr('selected','true');
		$('input[id=newUserSeq]').val('');
		$('input[id=newTitle]').val('');
		$('input[id=newScheduleFrDt]').val('');
		$('input[id=newScheduleToDt]').val('');
		$('input[id=newScheduleFrTime]').val('');
		$('input[id=newScheduleToTime]').val('');
		$('input[id=newScheduleHost]').val('');
		$('input[id=newSchedulePlace]').val('');
		$('input[id=newScheduleLinkUrl]').val('');
		$('textarea[id=newScheduleMemo]').val('');
	} else {
		alert("글작성은 로그인된 유저만 작성가능합니다.");
	}
}

function afterMakeScheduleFunctionOther(obj) {
	if(confirm("등록되었습니다! 페북에 등록하시겄수?")) {
		//글등록하기
		//페이스북
		var publish = {
			  method: 'feed',
			  message: $('input[id=newTitle]').val(),
			  name: 'Connect',
			  caption: '소셜캘린더화이팅',
			  description: ($('input[id=newScheduleMemo]').val()),
			  link: 'http://localhost:8080/socialtong/helloMain',
			  picture: 'https://ssl.gstatic.com/gb/images/j_e6a6aca6.png',
			  actions: [
			    { name: 'socialcalendar', link: 'http://localhost:8080/socialtong/helloMain' }
			  ],
			  user_message_prompt: 'Share your thoughts about socialc'
		};
		FB.ui(publish, function(){return false;});
		loadScheduleByCateClick()
		$('#mask, .window').hide();
	} else {
		loadScheduleByCateClick()
		$('#mask, .window').hide();
	}
}

function afterMakeScheduleFunction(obj) {
	alert("등록되었습니다!");
	loadScheduleByCateClick()
	$('#mask, .window').hide();
	$('#calendar').fullCalendar( 'refresh' );
}